/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/10/18
 */
layui.define(['form', 'layedit', 'laydate', 'upload','element'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let layedit = layui.layedit;
    let laydate = layui.laydate;
    let upload = layui.upload;
    let element = layui.element;

    form.render(null, "zmn-company-form");
    // 自定义验证规则
    form.verify({
        title: function (value) {
            if (value.length < 2) {
                return "不能少于2个字符";
            }
            ;
        },
        numAndAZ: function (value) {
            if (value == null || value == "") {
                return;
            }
            if (!new RegExp(/^[0-9A-Za-z]+$/).test(value)) {
                return "只能是数字和字母";
            }
        },
        mobile: function (value) {
            if (value == null || value == "") {
                return;
            }
            if (!new RegExp(/^[0-9]{11}$/).test(value)) {
                return "手机号只能是11位数字";
            }
        },
        idCard: function (value) {
            if (value == null || value == "") {
                return;
            }
            if (!new RegExp(/^[0-9]{17}[0-9X]{1}$/).test(value)) {
                return "身份证号只能是18位，且数字或者X结尾";
            }
        },
        num: function (value) {
            if (value == null || value == "") {
                return;
            }
            if (!new RegExp(/^[0-9]+$/).test(value)) {
                return "只能是数字";
            }
        },
        minSort: function(value, item){ //value：表单的值、item：表单的DOM对象
            if (value < 5000){
                return "排序号不能小于5000";
            }
        }
    });

    let company = json.company;
    // 服务商不允许修改信息
    if(company.type==130){
        $("input,textarea,select").prop("disabled", 'disabled').addClass("layui-disabled");
        $("#subBtn").hide();
    }
    let timestamp = new Date().getTime();
    //加载图片显示
    if (company != null) {
        reLoadImage('legalPersonIdcardFrontimgDiv', company.legalPersonIdcardFrontimg, "legalPersonIdcardFrontimg");
        reLoadImage("legalPersonIdcardBackimgDiv", company.legalPersonIdcardBackimg, "legalPersonIdcardBackimg");
        reLoadImage("businessLicenseImg1Div", company.businessLicenseImg1, "businessLicenseImg1");
        reLoadImage("businessLicenseImg2Div", company.businessLicenseImg2, "businessLicenseImg2");
        reLoadImage("businessLicenseImg3Div", company.businessLicenseImg3, "businessLicenseImg3");
        if (company.legalPersonIdcardFrontimg !== undefined && company.legalPersonIdcardFrontimg !== null) {
            timestamp = getImageTimestamp(company.legalPersonIdcardFrontimg);
        }
        if (company.legalPersonIdcardBackimg !== undefined && company.legalPersonIdcardBackimg !== null) {
            timestamp = getImageTimestamp(company.legalPersonIdcardBackimg);
        }
        if (company.businessLicenseImg1 !== undefined && company.businessLicenseImg1 !== null) {
            timestamp = getImageTimestamp(company.businessLicenseImg1);
        }
        if (company.businessLicenseImg2 !== undefined && company.businessLicenseImg2 !== null) {
            timestamp = getImageTimestamp(company.businessLicenseImg2);
        }
        if (company.businessLicenseImg3 !== undefined && company.businessLicenseImg3 !== null) {
            timestamp = getImageTimestamp(company.businessLicenseImg3);
        }
    }

    // 初始化文件上传插件
    if(company.type!=130){
        uploaderImage("legalPersonIdcardFrontimgDiv", "legalPersonIdcardFrontimg", 1);
        uploaderImage("legalPersonIdcardBackimgDiv", "legalPersonIdcardBackimg", 1);
        uploaderImage("businessLicenseImg1Div", "businessLicenseImg1", 2);
        uploaderImage("businessLicenseImg2Div", "businessLicenseImg2", 2);
        uploaderImage("businessLicenseImg3Div", "businessLicenseImg3", 2);
    }


    // 获取上一次上传图片的时间戳
    function getImageTimestamp(url) {
        var index = url.lastIndexOf("/");
        var str = url.substring(0, index);
        index = str.lastIndexOf("/");
        str = str.substring(index + 1, str.length);
        return str;
    }

    //取消按钮
    $("button.cancleBtn").click(function () {
        parent.layui.admin.events.closeThisTabs();
    });

    // 提交
    form.on('submit(zmn-company-form-submit)', function (obj) {
        var data = obj.field;
        let isEmpty = checkFromEmpty(data);
        if(isEmpty){
            layer.msg("尚未填写任何内容，不允许提交");
            return false;
        }
        var url = "/company/addEdit.action";

        var openId = layer.open({type: 3});

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify($.extend(json.company,data)),
            success: function (json) {
                layer.close(openId);
                layer.msg(json.message);
                if (json.status == 200) {
                    setTimeout(function () {
                        window.location.href="/company/companyFinance.action?companyId="+company.companyId+'&flag=3';
                    },1000);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
            }
        })
        return false;
    });

    /**
     * 是否没有填写表单直接提交
     * @param obj
     */
    function checkFromEmpty(obj){
        let isEmpty =true;
        for(let key in obj){
            if(key=="companyId"){
                continue;
            }
            if(obj[key]&&obj[key]!=''){
                isEmpty=false;
                break;
            }
        }
        return isEmpty;

    }

    /**
     * 描述: 图片上传方法
     * @param elemId
     * @param valueId
     */
    let indexUploadImags;

    function uploaderImage(elemId, valueId, type) {
        upload.render({
            elem: '#' + elemId
            , exts: 'jpg|png|jpeg'
            , acceptMime: 'image/*'
            , accept: 'images'
            , url: '/common/company/uploadLegalPersonImg?timestamp=' + new Date().getTime() + '&elemId=' + elemId + '&type=' + type
            , size: 3072
            , beforeSend: function (obj) {
                indexUploadImags = layui.layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            } ,
            done: function (res) {
                if (res.code == 2) {
                    var data = res.data;
                    $("#" + valueId).val(data.src);
                    $("#" + elemId).attr("style", "background:url( " + data.src + "?" + Date.now() + ");background-size:100% 100%;background-repeat:no-repeat;");
                    let next = $("#" + valueId).next();
                    let next1 = $(next).next();
                    $(next).attr("style", "visibility:hidden !important");
                    $(next1).attr("style", "visibility:hidden !important");
                } else {
                    layer.msg("上传失败！")
                }
                layui.layer.close(indexUploadImags);
            }
        });
    }

    function reLoadImage(elemId, value, valueId) {
        zmnUploadUtil.reLoadImage($, elemId, value, valueId);
    }

    //对外暴露的接口
    exports('art_company_material', {});
});